package io.renren.modules.hos.controller;

import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.app.form.RegisterForm;
import io.renren.modules.hos.entity.HosUserEntity;
import io.renren.modules.hos.form.HosRegisterForm;
import io.renren.modules.hos.service.HosUserService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysCaptchaService;
import io.renren.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RequestMapping("hos")
@Api("注册接口")
@RestController
public class HosRegisterController {

    @Autowired
    private HosUserService userService;
    @Autowired
    private SysCaptchaService sysCaptchaService;
    @Autowired
    private SysUserService sysUserService;

    @PostMapping("register")
    @ApiOperation("注册")
    public R Register(@RequestBody HosRegisterForm form){
        //表单校验
        ValidatorUtils.validateEntity(form);

        boolean captcha = sysCaptchaService.validate(form.getUuid(), form.getCaptcha());
        if(!captcha){
            return R.error("验证码不正确");
        }

        HosUserEntity user = userService.queryUserByPhone(form.getPhone());

        if (user != null){
            return R.error("该电话号码已被注册");
        }

        HosUserEntity hosUserEntity = new HosUserEntity();
        hosUserEntity.setPhone(form.getPhone());
        hosUserEntity.setPassword(form.getPassword());
        hosUserEntity.setAuthIdentity(0);
        hosUserEntity.setCreateTime(new Date());
        userService.save(hosUserEntity);

        return R.ok();
    }

    @PostMapping("register2")
    @ApiOperation("注册")
    public R Register2(@RequestBody HosRegisterForm form){
        //表单校验
        ValidatorUtils.validateEntity(form);

        boolean captcha = sysCaptchaService.validate(form.getUuid(), form.getCaptcha());
        if(!captcha){
            return R.error("验证码不正确");
        }

        SysUserEntity user = sysUserService.queryUserByMobile(form.getPhone());

        if (user != null){
            return R.error("该电话号码已被注册");
        }

        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setMobile(form.getPhone());
        sysUserEntity.setUsername(form.getPhone());
        sysUserEntity.setAuthIdentity(0);
        String salt = RandomStringUtils.randomAlphanumeric(20);
        sysUserEntity.setSalt(salt);
        sysUserEntity.setPassword(new Sha256Hash(form.getPassword(), salt).toHex());
        sysUserEntity.setCreateTime(new Date());
        sysUserService.save(sysUserEntity);

        return R.ok();
    }
}
